package com.haojii.twittertranslate;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import com.google.api.translate.Language;
import com.google.api.translate.Translate;

@SuppressWarnings("serial")
public class aoi_sola_Servlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(aoi_sola_Servlet.class.getName());
	
	private static final String LINE_BREAK = "<br/>";
	private static final String SCREENNAME = "aoi_sola_zh";
	private static final String PASSWORD = "Shift123ZZZ";
    
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");
		
		StringBuilder sb = new StringBuilder();
		String url = req.getRequestURL().toString();
		Translate.setHttpReferrer(url/* Enter the URL of your site here */); 
		
        sb.append(LINE_BREAK);
        try {            
            // Other methods require authentication
            Twitter twitter = new TwitterFactory().getInstance();
            
            /*String screenName = PropertiesConfiguration.getScreenName();
            String password = PropertiesConfiguration.getPassword();
            */
            
            
            Twitter aoi_sola_zh = new TwitterFactory().getInstance(SCREENNAME, PASSWORD);
            
            List<Status> statuses  = twitter.getUserTimeline("aoi_sola",new Paging(1, 2));
            log.info("-------START-------------------");
            log.info("Showing " + "aoi_sola" + "'s latest 2 status.");
            
            sb.append("-------START-------------------");
            sb.append(LINE_BREAK);
            sb.append("Showing " + "aoi_sola" + "'s latest 2 status.");
            sb.append(LINE_BREAK);
            for(int i=0;i<statuses.size();i++)
            {
            	Status status = statuses.get(i);
            	log.info(status.getUser().getName() +status.getCreatedAt()+ ":" + status.getText());
                long id = status.getId();
                String textSource = status.getText();
                String translatedText = "";
                boolean result = StatusEntityUtils.checkIfExist(id);
                if (!result)
                {
						//translatedText = Translate.execute(textSource,Language.JAPANESE, Language.CHINESE_SIMPLIFIED);
                		translatedText = ExciteTranslate.translateJA2CH(textSource);
                		if(!translatedText.isEmpty())
                		{
                			aoi_sola_zh.retweetStatus(id);
							aoi_sola_zh.updateStatus(translatedText);
							log.info("@aoi_sola by aoi_sola_zh "+translatedText);
							sb.append("@aoi_sola by aoi_sola_zh "+translatedText);
							sb.append(LINE_BREAK);
							StatusEntityUtils.save(id, status.getText());
                		}
                		else
                		{
                			log.log(Level.SEVERE, "ExciteTranslate.translateJA2CH didn't response");
                		}
                	
                }
            } 
            log.info("-----------END---------------");
            sb.append("-----------END---------------");
            sb.append(LINE_BREAK);
        } catch (TwitterException te) {
            log.info("TwitterException: " + te.getMessage());
            sb.append("TwitterException: " + te.getMessage());
            sb.append(LINE_BREAK);
        } catch (Exception e) {
        	log.info("UnknownException: " + e.getMessage());
            sb.append("UnknownException: " + e.getMessage());
            sb.append(LINE_BREAK);
        }
	 
		resp.getWriter().println(sb.toString()); 
	}
}
